﻿var xmlPath = ""; //XML文件地址，作为参数传递
var xmlDoc = null; //XML文档内容
var T = Array(); //阀值数组
var XMLimgPath = '../Images/Topo/operation/'; //操作权限控制中的操作图片基本路径
/*
*   加载XML文档
*   @param  filePath 文件相对路径
*/
function XMLLoad(filePath) {
    try {
        if (window.ActiveXObject) {
            xmlDoc = new ActiveXObject('Msxml2.DOMDocument.3.0');
            xmlDoc.async = false;
            xmlDoc.load(filePath);
        }
        else if (document.implementation && document.implementation.createDocument) {
            xmlDoc = document.implementation.createDocument('', '', null);
            xmlDoc.load(filePath);
        }
        else {
            xmlDoc = null;
        }
    }
    catch (e) {
        xmlDoc = null;
    }
    return xmlDoc;
}
//保存XML  
function saveXML(filePath) {
    xmlDoc.save(filePath);
}
/** 
* 解析XML文件函数 
* @param xmlDoc XML对象 
* @param name 属性名称 格式如:user.name 
*/
function getXMLProperty(xmlDoc, name) {
    var keys = name.split('.');
    var node = xmlDoc.documentElement;
    for (var i = 0; i < keys.length; i++) {
        var childs = node.childNodes;
        var key = keys[i];
        for (var k = 0; k < childs.length; k++) {
            var child = childs[k];
            if (child.nodeName == key) {
                if (child.childNodes.length == 1) {
                    return child.text;
                } else {
                    node = child;
                    break;
                }
            }
        }
    }
    return "";
}
//返回父节点(通过节点名字和节点值)  
function getNod(nam, val) {
    var node = xmlDoc.documentElement;
    var childs = node.childNodes;
    for (var i = 0; i < childs.length; i++) {
        var child = childs[i];
        var childms = child.childNodes;
        for (var k = 0; k < childms.length; k++) {
            var childm = childms[k];
            if (childm.nodeName == nam && (childm.text).substring(0, 6) == val) {
                return child;
            }
        }
    }
    return '';
}
//通过父节点和子节点名返回子节点值  
function getNodVal(nods, nam) {
    var childms = nods.childNodes;
    for (var k = 0; k < childms.length; k++) {
        var childm = childms[k];
        if (childm.nodeName == nam) {
            return childm.text;
        }
    }

    return '';
}
//通过父节点和子节点名返回子节点  
function getCurNod(nods, nam) {
    var childms = nods.childNodes;
    for (var k = 0; k < childms.length; k++) {
        var childm = childms[k];
        if (childm.nodeName == nam) { 
            return childm;
        }
    }

    return '';
}


/*
*   将XML转化为JavaScript对象数组
*   @param filePath XML文件相对路径
*   @param nodeName XMl文件节点名称 如 //Interval
*/
function XMLToJson(filePath,nodeName) {
    XMLLoad(filePath);//加载XML文件
   return RecursiveGetChildNode(xmlDoc.selectNodes(nodeName));
}
/*
*   递归获取子节点值
*/
function RecursiveGetChildNode(childObjs) {
    //var jsonStr = "";
    T = [];
    var length = childObjs.length;
    for (var i = 0; i < length; i++) {
        var threshold = new Object();  
        threshold.value = childObjs[i].selectSingleNode("value").text;
        threshold.color = childObjs[i].selectSingleNode("color").text;
        T.push(threshold);
        //var length1 = childObjs[i].childNodes.length;
//        threshold.value = childObjs[i].selectSingleNode("value").text;
//        threshold.color = childObjs[i].selectSingleNode("color").text;
//        t.push(threshold);
//        t[i].value = childObjs[i].selectSingleNode("value").text;
//        t[i].color = childObjs[i].selectSingleNode("color").text;
//        jsonStr += "{";
//        for (var j = 0; j < length1; j++) {
//            jsonStr += "\"" + childObjs[i].childNodes[j].nodeName + "\":\"" + childObjs[i].childNodes[j].text + "\"";
//        }
//        if (i == length - 1) {
//            jsonStr += "}";
//        }
//        else {
//            jsonStr += "},";
//        }
    }
    //return jsonStr;
    return T;
}


/*
*   下面函数针对于VML权限控制
*/
/*
*   节点对象
*   {
*       operantion:'',
*       {
*           id:'',
*           name:'',
*           action:'',
*           tip:'',
*           ico:'' 
*        }
*   }
*   
*/

/*
*   将节点转化为对象
*   @param  node    操作节点
*/
function getNodeObj(node) {
    var o = new Object();
    if (node.nodeName == 'operation') {
        o.operationdid = node.getAttribute("value");
        var length = node.childNodes.length;
        for (var i = 0; i < length; i++) {
            switch (node.childNodes[i].nodeName.toUpperCase()) {
                case 'NAME':
                    o.name = node.childNodes[i].text;
                    break;
                case 'ACTION':
                    o.action = node.childNodes[i].text;
                    break;
                case 'TIP':
                    o.tip = node.childNodes[i].text;
                    break;
                case 'ICO':
                    o.ico = node.childNodes[i].text;
                    break;
                case 'ISDISPLAY':
                    o.isdisplay = node.childNodes[i].text;
                    break;
            }
        }
    }
    return o;
}
/*
*   获取属性值节点
*   @param  filePath    XML文件路径
*   @param  nodename    节点名称
*/
function getXMLPropertyNodes(filePath, nodename) {
    if (xmlDoc == null) { //如果还没有加载XML文件
        XMLLoad(filePath); //加载XML文件
    }
    if (xmlDoc.parseError.errorCode == 0) { //errorCode为0说明XML文件打开没有错误
        var nodes = xmlDoc.selectSingleNode(nodename); //xmlDoc.parseError.errorCode
        var o = new Object();
        if (nodes.nodeName == 'operations') {
            var length = nodes.childNodes.length;
            for (var i = 0; i < length; i++) {
                if (nodes.childNodes[i].selectSingleNode('isdisplay').text == '0') {//如果该节点可以在界面上显示
                    o[i] = getNodeObj(nodes.childNodes[i]);
                }
            }
        }
        else {
            o[0] = getNodeObj(nodes);
        }
        return o;
    }
    else {
        return null;
    }
}

/*
*   获取属性值节点
*   @param  fp    XML文件路径
*   @param  n    节点名称
*   @param  v   节点值
*/
function getXMLPropertyNode(fp, parent, n, v) {
    var operations = null;
    if (parent == '') { parent = 'operations'; }
    operations = getXMLNodes(fp, parent); //获取所有的操作
    if (operations) {//如果操作不为空
        var nodes = getXMLNodes(fp, n); //获取匹配节点组
        var o = [];
        var length = nodes.childNodes.length;
        for (var i = 0; i < length; i++) {
            if (nodes.childNodes[i].getAttribute('value') == v) {//获取该设备下的操作
                var count = nodes.childNodes[i].childNodes.length;
                var num = operations.childNodes.length;
                for (var j = 0; j < count; j++) {
                    for (var k = 0; k < num; k++) {
                        if (nodes.childNodes[i].childNodes[j].text == operations.childNodes[k].getAttribute('value')) {
                            o.push(getNodeObj(operations.childNodes[k]));
                            break;
                        }
                    }
                }
                break;
            }
        }
        return o;
    }
    else {
        return null;
    }
}

/*
*   获取节点数组
*   @param  fp  XML文件路径
*   @param  n   需要获取的节点名称
*/
function getXMLNodes(fp, n) {
    var nodes = null;//返回节点
    if (xmlDoc == null) { //如果还没有加载XML文件
        XMLLoad(fp); //加载XML文件
    }
    if (xmlDoc.parseError.errorCode == 0) { //errorCode为0说明XML文件打开没有错误
        nodes = xmlDoc.selectSingleNode('//' + n);
    }
    return nodes;
}
